package com.sczmgk.hotline_pzh.base;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;

import com.sczmgk.hotline_pzh.R;
import com.sczmgk.hotline_pzh.callback.CustomViewInterf;
import com.sczmgk.hotline_pzh.callback.HandlerCallback;
import com.sczmgk.hotline_pzh.popup.PupDialog;
import com.sczmgk.hotline_pzh.ui.MainActivity;
import com.sczmgk.hotline_pzh.utils.MyLog;
import com.sczmgk.hotline_pzh.view.MyHeadView;


/**
 * 自定义主页Viewpager内容基类
 * 1.定义生命周期
 * 2.定义handler
 * 3.定义公用title
 */
public abstract class NewsBaseView implements CustomViewInterf {
    protected  final String TAG = NewsBaseView.this.getClass().getSimpleName();
    protected MainActivity activity;
    protected Dialog loadingDialog;
    /**
     * 自定义title
     */
    private MyHeadView mHeadView;
    private Context context;
    /**
     * 新闻容器
     */
    protected View rootView;
    /**
     * 最外层的容器
     */
    protected FrameLayout container;
    protected Handler mHandler;
    protected boolean isResume;
    protected boolean isPause;
    protected boolean isStop;
    protected boolean isDestroy;
    protected boolean isCreate;

    /**
     * 主页Handler消息回调
     */
    protected HandlerCallback handlerCallback = new HandlerCallback() {
        @Override
        public void onHandleMessage(Message msg) {
            MyLog.d(TAG, "MainHomeBAseView收到消息----------运行的类是：" + NewsBaseView.this.getClass());
            mHandleMessage(msg);
        }
    };

    /**
     * 由子类实现
     * @param msg
     */
    public abstract void mHandleMessage(Message msg);

    public NewsBaseView(Activity context) {
        this.context = context;
        LayoutInflater inflater = LayoutInflater.from(context);
        rootView =  inflater.inflate(R.layout.layout_include_title,null);
        container = (FrameLayout)rootView.findViewById(R.id.main_home_container);
        mHeadView = (MyHeadView)rootView.findViewById(R.id.headview);
        mHeadView.setVisibility(View.GONE);
        mHeadView.setLeftBtnBackground(R.drawable.ic_menu);
        activity = (MainActivity)context;
        this.mHandler = activity.getmHandler();
    }

    public void showHead(){
        mHeadView.setVisibility(View.VISIBLE);
    }
    /**
     * 取得实例化的View
     * @return
     */
    public View getView(){
        return rootView;
    }

    /**
     * 初始化View
     */
    public abstract void initView();
    /**
     * 初始化头布局
     */

    public void initHeadView() {

         setLeftBtnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 //设置点击事件

                 MainActivity activity = (MainActivity) context;
//                 activity.toggle();
             }
         });
    }

    /**
     * 显示title头
     */
    @Override
    public void visibleHeadView() {

        mHeadView.setVisibility(View.VISIBLE);
    }


    /**
     * 设置左边按钮显示状态
     *
     * @param enabled
     */
    @Override
    public void setLeftBtnEnabled(boolean enabled) {

    }

    /**
     * 设置左边按钮显示状态
     *
     * @param visible
     */
    @Override
    public void setLeftBtnVisibility(int visible) {

        mHeadView.setLeftBtnVisibility(visible);
    }

    /**
     * 设置左边按钮背景
     *
     * @param resource
     */
    @Override
    public void setLeftBtnBackground(int resource) {

        mHeadView.setLeftBtnBackground(resource);
    }

    /**
     * 设置左边按钮背景
     *
     * @param resource
     * @param text
     */
    @Override
    public void setLeftBtnBackground(int resource, int text) {

        mHeadView.setLeftBtnBackground(resource, text);
    }

    /**
     * 设置左边按钮背景
     *
     * @param resource
     * @param text
     * @param textColorResource
     */
    @Override
    public void setLeftBtnBackground(int resource, int text, int textColorResource) {

        mHeadView.setLeftBtnBackground(resource, text, textColorResource);
    }

    /**
     * 获取左边按钮的背景资源ID
     *
     * @param textColorResource
     */
    @Override
    public void setLeftBtnTextColor(int textColorResource) {

        mHeadView.setLeftBtnTextColor(textColorResource);
    }

    /**
     * 设置左边按钮的文字
     *
     * @param text
     */
    @Override
    public void setLeftBtnText(String text) {

        setLeftBtnText(text);
    }

    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     *
     * @param listener
     */
    @Override
    public void setLeftBtnClickListener(View.OnClickListener listener) {

        mHeadView.setLeftBtnClickListener(listener);
    }

    /**
     * 设置左边按钮显示状态
     *
     * @param enabled
     */
    @Override
    public void setRightBtnEnabled(boolean enabled) {

        //未实现
    }

    /**
     * 设置右边按钮显示状态
     *
     * @param visible
     */
    @Override
    public void setRightBtnVisibility(int visible) {

        mHeadView.setRightBtnVisibility(visible);
    }


    /**
     * 设置右边按钮背景
     *
     * @param resource
     */
    @Override
    public void setRightBtnBackground(int resource) {

        mHeadView.setRightBtnBackground(resource);
    }

    /**
     * 设置右边按钮描述
     *
     * @param desc
     */
    @Override
    public void setRightBtnDescription(String desc) {

        mHeadView.setRightBtnDesc(desc);
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     * @param text
     */
    @Override
    public void setRightBtnBackground(int resource, int text) {

        mHeadView.setRightBtnBackground(resource, text);
    }

    /**
     * 设置右边按钮背景
     *
     * @param resource
     * @param text
     * @param textColorResource
     */
    @Override
    public void setRightBtnBackground(int resource, int text, int textColorResource) {

        mHeadView.setRightBtnBackground(resource, text, textColorResource);
    }

    /**
     * 获取左边按钮的背景资源ID
     *
     * @param textColorResource
     */
    @Override
    public void setRightBtnTextColor(int textColorResource) {

        mHeadView.setRightBtnTextColor(textColorResource);
    }


    /**
     * 设置右边按钮的文字
     *
     * @param text
     */
    @Override
    public void setRigthBtnText(String text) {

    }
    /**
     * 设置右边按钮文字
     *
     * @param resid
     */
    @Override
    public void setRightBtnText(int resid) {

        mHeadView.setRightBtnText(resid);
    }


    public void setRightBtnText(String text){

        mHeadView.setRightBtnText(text);
    }
    /**
     * 设置左边按钮背景 一旦为左边按钮设置监听，将会导致无法切换menu菜单
     *
     * @param listener
     */
    @Override
    public void setRightBtnClickListener(View.OnClickListener listener) {

        mHeadView.setRightBtnClickListener(listener);
    }

    /**
     * 设置标题icom
     *
     * @param resource
     */
    @Override
    public void setTitleIcon(int resource) {

        mHeadView.setTitleIcon(resource);
    }

    /**
     * 设置标题
     *
     * @param resource
     */
    @Override
    public void setTitle(int resource) {

        mHeadView.setTitle(resource);
    }

    /**
     * 设置标题
     *
     * @param title
     */
    @Override
    public void setTitle(String title) {

        mHeadView.setTitle(title);
    }

    /**
     * 设置标题颜色
     *
     * @param color
     */
    @Override
    public void setTitleColor(int color) {

        mHeadView.setTitleColor(color);
    }

    /**
     * 设置头部背景
     *
     * @param color
     */
    @Override
    public void setHeadBackgroudColor(int color) {

        mHeadView.setHeadBackground(color);
    }

    /**
     * 设置标题栏显示状态
     *
     * @param visible
     */
    @Override
    public void setHeadViewVisibility(int visible) {

       mHeadView.setVisibility(visible);
    }

    /**
     * 显示底部弹出框
     *
     * @param pupid    弹出框的 id
     * @param pupEvent 弹出框 按钮的监听事件接口
     */
    @Override
    public void showPup(String pupid, PupDialog.PupEvent pupEvent) {

    }
    public void onCreate() {

        isCreate = true;
        isPause = false;
        isDestroy = false;
        isResume = false;
        isStop = false;
    }


    public void onDestroy() {

        isDestroy = true;
        isPause = false;
        isCreate = false;
        isStop = false;
        isResume = false;
        if(loadingDialog != null){
            if(loadingDialog.isShowing()){
                loadingDialog.dismiss();
                loadingDialog = null;
            }
        }
    }


    public void onPause() {

        isPause = true;
        isCreate = false;
        isResume = false;
        isStop = false;
        isDestroy = false;

    }


    public void onResume() {
        MyLog.d(TAG,NewsBaseView.this.getClass().getSimpleName()+"   onResume");
        isResume = true;
        isCreate = false;
        isPause = false;
        isStop = false;
        isDestroy = false;
        activity.setHandlerCallback(handlerCallback);
    }


    public void onStop() {

        isStop = true;
        isCreate = false;
        isResume = false;
        isStop = false;
        isDestroy = false;

    }
}
